/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: list_operate.c
> Author:MOKI
> Mail:1913964829@qq.com 
> Created Time: 2024年09月13日 星期五 18时24分21秒
***************************************************/
#include "global.h"
DataList *createEmployeeList()
{
    DataList *head = NULL, *node = NULL, *end = NULL;
    size_t rdf = 0;
    head = (DataList *)malloc(sizeof(DataList));
    memset(head, 0, sizeof(DataList));
    end = head;
    head->pn = NULL;
    FILE *fp = fopen(EMPLOYEES_FILE_PATH, "r");
    rdf = fread(&(head->datatype.employee), sizeof(Employee), 1, fp);
    while (rdf == 1)
    {
        node = (DataList *)malloc(sizeof(DataList));
        memset(node, 0, sizeof(DataList));
        rdf = fread(&(node->datatype.employee), sizeof(Employee), 1, fp);
        if (rdf == 1)
        {
            end->pn = node;
            end = node;
            end->pn = NULL;
        }
    }
    free(node);
    node = NULL;
    fclose(fp);
    return head;
}
DataList *createVisitorList()
{
    DataList *head = NULL, *node = NULL, *end = NULL;
    size_t rdf = 0;
    head = (DataList *)malloc(sizeof(DataList));
    memset(head, 0, sizeof(DataList));
    end = head;
    head->pn = NULL;
    FILE *fp = fopen(VISITORS_FILE_PATH, "r");
    rdf = fread(&(head->datatype.visitor), sizeof(Visitor), 1, fp);
    while (rdf == 1)
    {
        node = (DataList *)malloc(sizeof(DataList));
        memset(node, 0, sizeof(DataList));
        rdf = fread(&(node->datatype.employee), sizeof(Visitor), 1, fp);
        if (rdf == 1)
        {
            end->pn = node;
            end = node;
            end->pn = NULL;
        }
    }
    free(node);
    node = NULL;
    fclose(fp);
    return head;
}
DataList *createPlantList()
{
    DataList *head = NULL, *node = NULL, *end = NULL;
    size_t rdf = 0;
    head = (DataList *)malloc(sizeof(DataList));
    memset(head, 0, sizeof(DataList));
    end = head;
    head->pn = NULL;
    FILE *fp = fopen(PLANTS_FILE_PATH, "r");
    rdf = fread(&(head->datatype.plant), sizeof(Plant), 1, fp);
    while (rdf == 1)
    {
        node = (DataList *)malloc(sizeof(DataList));
        memset(node, 0, sizeof(DataList));
        rdf = fread(&(node->datatype.employee), sizeof(Plant), 1, fp);
        if (rdf == 1)
        {
            end->pn = node;
            end = node;
            end->pn = NULL;
        }
    }
    free(node);
    node = NULL;
    fclose(fp);
    return head;
}
DataList *createSurveillanceDataList()
{
    DataList *head = NULL, *node = NULL, *end = NULL;
    size_t rdf = 0;
    head = (DataList *)malloc(sizeof(DataList));
    memset(head, 0, sizeof(DataList));
    end = head;
    head->pn = NULL;
    FILE *fp = fopen(SURVEILLANCE_DATA_FILE_PATH, "r");
    rdf = fread(&(head->datatype.surveillanceData), sizeof(SurveillanceData), 1, fp);
    while (rdf == 1)
    {
        node = (DataList *)malloc(sizeof(DataList));
        memset(node, 0, sizeof(DataList));
        rdf = fread(&(node->datatype.surveillanceData), sizeof(SurveillanceData), 1, fp);
        if (rdf == 1)
        {
            end->pn = node;
            end = node;
            end->pn = NULL;
        }
    }
    free(node);
    node = NULL;
    fclose(fp);
    return head;
}
DataList *createEnvironmentDataList()
{
    DataList *head = NULL, *node = NULL, *end = NULL;
    size_t rdf = 0;
    head = (DataList *)malloc(sizeof(DataList));
    memset(head, 0, sizeof(DataList));
    end = head;
    head->pn = NULL;
    FILE *fp = fopen(ENVIRONMENT_DATA_FILE_PATH, "r");
    rdf = fread(&(head->datatype.environmentData), sizeof(EnvironmentData), 1, fp);
    while (rdf == 1)
    {
        node = (DataList *)malloc(sizeof(DataList));
        memset(node, 0, sizeof(DataList));
        rdf = fread(&(node->datatype.environmentData), sizeof(EnvironmentData), 1, fp);
        if (rdf == 1)
        {
            end->pn = node;
            end = node;
            end->pn = NULL;
        }
    }
    free(node);
    node = NULL;
    fclose(fp);
    return head;
}
int listLen(DataList *head)
{
    DataList *end = head;
    int count = 0;
    while (end != NULL)
    {
        count++;
        end = end->pn;
    }
    return count;
}
void freeList(DataList *head)
{
    DataList *end = NULL;
    end = head;
    while (end != NULL)
    {
        end = head->pn;
        free(head);
        head = end;
    }
}